﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity.Layout;

namespace TestApp
{
    public class DRectangle:Shape
    {
        private Rectangle rect; //声明一个矩形引用 
        private Point origPoint; //设置一个鼠标点的引用 
        private bool isAddMouseEvent = false; //标示是否添加了鼠标事件 
        private Canvas _parent;
        private bool IsCompleted;
        MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior(); 
        public DRectangle() {
            this.Loaded += (s, e) => InitVisualize();
            //dragBehavior.Attach(rect);

        } 

        void dragBehavior_Dragging(object sender, MouseEventArgs e)
        {
            MouseDragElementBehavior dragBehavior_temp = sender as MouseDragElementBehavior;

            //获取画布左上顶点坐标
            double canvas1_left = Canvas.GetLeft(_parent);
            double canvas1_top = Canvas.GetTop(_parent);

            //限制拖放控件的移动范围 

            if (dragBehavior_temp.X < canvas1_left)
            {
                dragBehavior_temp.X = canvas1_left;
            }

            if (dragBehavior_temp.X > canvas1_left + _parent.Width)
            {
                dragBehavior_temp.X = _parent.Width;
            }

            if (dragBehavior_temp.Y < canvas1_top)
            {
                dragBehavior_temp.Y = canvas1_top;
            }

            if (dragBehavior_temp.Y > canvas1_top + _parent.Height)
            {
                dragBehavior_temp.Y = _parent.Height;
            } 
        }


        void dragBehavior_DragFinished(object sender, MouseEventArgs e)
        {
            MouseDragElementBehavior dragBehavior_temp = sender as MouseDragElementBehavior;

            //获取移入对象的画布坐标
            double rectangle1_left = Canvas.GetLeft(_parent);
            double rectangle1_top = Canvas.GetTop(_parent);

            //判断移入对象的范围 
            if (
            (dragBehavior_temp.X > (rectangle1_left - _parent.Width) && dragBehavior_temp.X < (rectangle1_left + _parent.Width))
            && (dragBehavior_temp.Y > (rectangle1_top - _parent.Height) && dragBehavior_temp.Y < (rectangle1_top + _parent.Height))
            )
            {

                //图形吸附到移入对象
                dragBehavior_temp.X = rectangle1_left;
                dragBehavior_temp.Y = rectangle1_top;

                //吸附后失去拖放行为绑定,并注销拖放完成事件
                //dragBehavior_temp.Detach();
                //dragBehavior_temp.DragFinished -= new MouseEventHandler(dragBehavior_DragFinished); 
            }
            else
            {
                return;
            }
        }

        private void InitVisualize()
        {
            try
            {
                _parent = this.Parent as Canvas;
            }
            catch (Exception)
            {
                throw new Exception("DRectangle 的父元素应该指定为Canvas类型！");
            }
            //rect = new Rectangle();
            //_parent.Children.Add(rect);
            _parent.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseLeftButtonDown);
            _parent.MouseMove += new MouseEventHandler(Handle_MouseMove);
            _parent.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseLeftButtonUp);
            
        }
        private void Handle_MouseMove(object sender, MouseEventArgs e)
        {
            rect = new Rectangle();
            Point curPoint = e.GetPosition(_parent);
            if (curPoint.X > origPoint.X)
            {
                rect.Width = curPoint.X - origPoint.X;
            }
            if (curPoint.X < origPoint.X)
            {
                rect.SetValue(Canvas.LeftProperty, curPoint.X);
                rect.Width = origPoint.X - curPoint.X;
            }
            if (curPoint.Y > origPoint.Y)
            {
                rect.Height = curPoint.Y - origPoint.Y;
            }
            if (curPoint.Y < origPoint.Y)
            {
                rect.SetValue(Canvas.TopProperty, curPoint.Y);
                rect.Height = origPoint.Y - curPoint.Y;
            }
        }

        private void Handle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _parent.MouseLeftButtonUp -= Handle_MouseLeftButtonUp;
            _parent.MouseMove -= Handle_MouseMove;
        }

        private void Handle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            rect = new Rectangle();
            origPoint = e.GetPosition(_parent);
            rect.SetValue(Canvas.LeftProperty, origPoint.X); //设置矩形的起始X坐标 
            rect.SetValue(Canvas.TopProperty, origPoint.Y);  //设置矩形的起始Y坐标 
            rect.Opacity = 1;          //设置本控件透明度 
            rect.Fill = new SolidColorBrush(Colors.Blue);
            rect.RadiusX = 10;//为了让矩形美观一些设置了圆角属性 
            rect.RadiusY = 10;
           
            //rect.MouseMove += new MouseEventHandler(rect_MouseMove);

            dragBehavior.DragBegun += new MouseEventHandler(dragBehavior_DragBegun); //注册开始拖放的事件处理方法
            dragBehavior.Dragging += new MouseEventHandler(dragBehavior_Dragging); //注册拖放过程的事件处理方法
            dragBehavior.DragFinished += new MouseEventHandler(dragBehavior_DragFinished); //注册完成拖放事件处理方法
            //_parent.MouseMove += Handle_MouseMove; //为Canvas面板加载MouseMove事件 
            //_parent.MouseLeftButtonUp += Handle_MouseLeftButtonUp;//为Canvas面板加载MouseLeftButtonUp事件 
            _parent.Children.Add(rect); //在Canvas面板中添加矩形 
        }

        private void dragBehavior_DragBegun(object sender, MouseEventArgs e)
        {
            dragBehavior.ConstrainToParentBounds = true; 
        }

        private void rect_MouseMove(object sender, MouseEventArgs e)
        {
            //origPoint = e.GetPosition(_parent);
            //rect.SetValue(Canvas.LeftProperty, origPoint.X);
            //rect.SetValue(Canvas.TopProperty, origPoint.Y);
        }

        private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            rect.Stroke = new SolidColorBrush(Colors.Gray);
            rect.StrokeThickness = 3;
        }
    }
}
